package com.example.demo

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.navigation.NavGraphBuilder
import com.example.chat.ChatFeatureSDK
import com.example.chat.chatNestedNavigationGraph
import com.example.contact.ContactFeatureSDK
import com.example.contact.contactNestedNavigationGraph
import com.example.discover.DiscoverFeatureSDK
import com.example.discover.discoverNestedNavigationGraph
import com.example.featuresdk.core.NavigationMediator
import com.example.featuresdk.viewmodel.NavigationViewModel
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
class FeatureModule {

    @Provides
    @Singleton
    fun navigationMediator(): NavigationMediator {

        Log.i("$this","Creating NavigationMediator")

        return NavigationMediator().apply {
            register(ChatFeatureSDK(this))
            register(ContactFeatureSDK(this))
            register(DiscoverFeatureSDK(this))
        }
    }
}

//Guide:https://developer.android.google.cn/guide/navigation/navigation-nested-graphs
@OptIn(ExperimentalUnitApi::class)
@ExperimentalFoundationApi
fun NavGraphBuilder.buildNestedNavigationGraph(navigationViewModel: NavigationViewModel) {

    navigationViewModel.features.value.forEach { (key, featureSdk) ->
        when (key) {
            ChatFeatureSDK.FEATURE_KEY -> chatNestedNavigationGraph(navigationViewModel, featureSdk)
            ContactFeatureSDK.FEATURE_KEY -> contactNestedNavigationGraph(navigationViewModel, featureSdk)
            DiscoverFeatureSDK.FEATURE_KEY -> discoverNestedNavigationGraph(navigationViewModel, featureSdk)
        }
    }
}
